#include "condition.h"


std::string Condition::toString(){
    if(!operands.empty()){
        std::string opStr = op=="and"? "&&":"||";
        std::string res = "(";
        for(auto& temp:operands){
            res+=temp->toString()+opStr;
        }
        res.erase(res.length()-opStr.length());
        return res+")";
    }else{
        return field + " " + op + " " + value;
    }
}

bool Condition::evaluate(const std::vector<std::vector<std::string>> &record){
    if(!operands.empty()){
        if(op == "and"){
            for(auto& temp:operands){
                if(!temp->evaluate(record)){
                    return false;
                }
            }
            return true;
        }else if(op=="or"){
            for(auto& temp:operands){
                if(temp->evaluate(record)){
                    return true;
                }
            }
            return false;
        }
        if(op==""){
            return operands[0]->evaluate(record);
        }
    }else {
        field = QString::fromStdString(field).trimmed().toStdString();
        value = QString::fromStdString(value).trimmed().toStdString();
        for(auto each:User1::instance()->globalManager.users[User1::instance()->userIndex].tables[User1::instance()->currentTableIndex].attributes){
            if(each.attriname==field){
                if(each.type==4){
                    if(op=="like"){
                        QRegExp find1("%");
                        QRegExp find2("\\*");
                        QString regexp = QString::fromStdString(value);
                        regexp.replace(find1,"(.+)");
                        regexp.replace(find2,"(.*)?");
                        QRegExp check(regexp);
                        if(check.indexIn(QString::fromStdString(record[1][indexOf(field,record[0])]))!=-1){
                            return true;
                        }else{
                            return false;
                        }
                    }
                    cout<<"Cannot compare the type of varchar"<<endl;
                    return false;
                }else{
//                    if((each.type==1 ||each.type==2)&& op =="between"){
//                        QString Value = QString::fromStdString(value);
//                        QStringList valueList = Value.split(",");
//                        float first = valueList[0].toFloat();
//                        float second = valueList[1].toFloat();
//                        cout<<record[1][indexOf(field,record[0])]<<endl;
//                        if(record[1][indexOf(field,record[0])]==""||record[1][indexOf(field,record[0])]=="null"){
//                            return false;
//                        }
//                        return  stof(record[1][indexOf(field,record[0])])>=first &&  stof(record[1][indexOf(field,record[0])])<=second;
//                    }
                    if(op==">"){return stof(record[1][indexOf(field,record[0])]) > stof(value); }
                    if(op=="<"){return stof(record[1][indexOf(field,record[0])]) < stof(value); }
                    if(op==">="){return stof(record[1][indexOf(field,record[0])]) >= stof(value); }
                    if(op=="<="){return stof(record[1][indexOf(field,record[0])]) <= stof(value); }
                    if(op=="="){return stof(record[1][indexOf(field,record[0])]) == stof(value); }
                    if(op=="!="){return stof(record[1][indexOf(field,record[0])]) != stof(value); }
                    if(op=="like"){
                        return false;
                    }
                }
            }
        }
    }
}


int Condition::indexOf(const std::string& field, const std::vector<std::string>& header) {
       for (int i = 0; i < header.size(); i++) {
           if (header[i] == field) {
               return i;
           }
       }
       return -1;
   }